<?php
// +----------------------------------------------------------------------
// | Author: 发仔 <i@fazi.me> 
// +----------------------------------------------------------------------

namespace app\admin\controller;

use think\Controller;
use think\Db;

class Beidou extends Base
{
	public function index() {
		return $this->out();
	}
	
	//所有车辆
	public function cars() {
		
		$json = file_get_contents('http://new.jjcxfw.com/api/driver.index/getredisbeidou');
		$json = json_decode($json,1);
		
//		halt($json);
		if($json['code'] == 1) {
//			
//			$data = array_column($json['data'],null,'driver_id');
//			$ids = array_column($json['data'],'driver_id');
//			
			$data = array_column($json['data'],null,'carno');
			$ids = array_column($json['data'],'carno');
			
			//
			$where = [];
			$number = input('param.number','','urldecode');
			$where['c.number'] = $number ? ['like',"%{$number}%"] : ['IN',$ids];
			
			//数据库
			$list = Db::name('driver')->alias('m')->join('car c','m.car_id = c.car_id')->where('m.car_id','>',0)->where($where)->field('m.driver_id,m.car_id,m.name,c.number,m.phone,m.work_state')->select();
			
			foreach($list AS $id => $value) {
				$coords = $data[$value['number']];
				$value['loc'] = [$coords['lon'],$coords['lat']];
				$list[$id] = $value;
			}
//			halt($list);
			return $this->success('数据请求成功。','',$list);
			
		} else {
			return $this->error('数据请求异常，请重新发起请求。');
		}
	}
	
	//车辆查订单
	public function orders() {
		
		$id = input('param.id/d');
		$page = input('param.page/d',1);
		$size = input('param.pagesize/d');
		
		$list = Db::name('order')->alias('o')
				->join('order_fee of','of.order_id = o.order_id')
				->join('order_gps og','og.order_id = o.order_id')
				->join('driver d','d.driver_id = o.driver_id')
				->where('o.car_id',$id)->field('o.*,d.name AS driver_name,of.order_money,og.from_lon,og.from_lat')->page($page)->limit($size)->select();
		
		if($list) {
			return $this->success('数据请求成功。','',$list);
		}
		return $this->error('数据请求异常，请重新发起请求。');
		
	}
	
	public function getCurrentLoc() {
		
		$car_number = input('param.car_number/s');
		
		$json = file_get_contents('http://new.jjcxfw.com/api/driver.index/getredisbeidou');
		$json = json_decode($json,1);
		
		$data = array_column($json['data'],null,'carno');
		
		return $this->apiout($data[$car_number],$data[$car_number]);
		
	}
}
